#!/usr/bin/env python3

# Copyright (c) 2020-2025 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


from ..cfg._cfg_dir_sys import SysCfgDir
from ..cfg._cfg_dir_user import UserCfgDir
from ..cfg._service_daemon import DaemonService as CfgDaemonService
from ..cfg._service_automount import AutoMountService as CfgAutoMountService
from ..cfg._service_mount import MountService as CfgMountService
from ..cfg._service_swap import SwapService as CfgSwapService
from ._util import RunUtil


class Service:

    # see ../cfg/_service.py

    def __init__(self, cfg):
        self._cfg = cfg

    @property
    def cfg(self):
        return self._cfg

    @property
    def name(self):
        return self._cfg.name

    @property
    def unit_name(self):
        return self._cfg.unit_name

    @property
    def unit_file_path(self):
        return self._cfg.unit_file_path

    @property
    def unit_file_content(self):
        return self._cfg.unit_file_content

    @property
    def description(self):
        return self._cfg.description

    def is_enabled(self):
        return self._cfg.is_enabled()

    def enable(self):
        return self._cfg.enable()

    def disable(self):
        return self._cfg.disable()

    def is_running(self):
        return RunUtil.systemdIsUnitRunning(self._cfg.unit_name)

    def start(self):
        RunUtil.systemdStartUnit(self._cfg.unit_name)

    def stop(self):
        RunUtil.systemdStopUnit(self._cfg.unit_name)

    def remove_and_dispose(self):
        assert not self.is_running()
        self._cfg.remove_and_dispose()

    @staticmethod
    def _assertSystemOrUser(system_or_user, cfg):
        if system_or_user:
            assert isinstance(cfg._parent, SysCfgDir)
        else:
            assert isinstance(cfg._parent, UserCfgDir)


class DaemonService(Service):

    def __init__(self, system_or_user, name=None, cfg=None):
        if name is not None:
            assert cfg is None
            cfg = CfgDaemonService(SysCfgDir() if system_or_user else UserCfgDir(), name=name)
            super().__init__(cfg)
        elif cfg is not None:
            self._assertSystemOrUser(system_or_user, cfg)
            super().__init__(cfg)
        else:
            assert False


class AutoMountService(Service):

    def __init__(self, system_or_user, name=None, cfg=None):
        if name is not None:
            assert cfg is None
            cfg = CfgAutoMountService(SysCfgDir() if system_or_user else UserCfgDir(), name=name)
            super().__init__(cfg)
        elif cfg is not None:
            self._assertSystemOrUser(system_or_user, cfg)
            super().__init__(cfg)
        else:
            assert False


class MountService(Service):

    def __init__(self, system_or_user, name=None, cfg=None):
        if name is not None:
            assert cfg is None
            cfg = CfgMountService(SysCfgDir() if system_or_user else UserCfgDir(), name=name)
            super().__init__(cfg)
        elif cfg is not None:
            self._assertSystemOrUser(system_or_user, cfg)
            super().__init__(cfg)
        else:
            assert False


class SwapService(Service):

    def __init__(self, system_or_user, name=None, cfg=None):
        if name is not None:
            assert cfg is None
            cfg = CfgSwapService(SysCfgDir() if system_or_user else UserCfgDir(), name=name)
            super().__init__(cfg)
        elif cfg is not None:
            self._assertSystemOrUser(system_or_user, cfg)
            super().__init__(cfg)
        else:
            assert False
